import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class ChooseTokenGUI extends JPanel implements ActionListener {	
	
	private Board board = new Board();
	private Token token = new Token();
	
	static JFrame frame;
	static JTextField[] playerName;
	static JComboBox[] tokenList;
	static JButton okayButton;
	static JButton cancelButton;
	static JPanel mainPanel;
	
	public ChooseTokenGUI(int x) {
		
		super(new BorderLayout());
		
		frame = new JFrame("Players Setup");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		board.setPlayerCount(x);
		board.setActivePlayers(x);
		
		playerName = new JTextField[x];
		tokenList = new JComboBox[x];
		mainPanel = new JPanel(new GridLayout(0, 2, 5, 3));
		
		for (int y = 0; y < x; y++) {
		
			playerName[y] = new JTextField("Player " + (y + 1));
			playerName[y].addActionListener(this);
			mainPanel.add(playerName[y]);
			
			tokenList[y] = new JComboBox(token.getAllNames());
			tokenList[y].addActionListener(this);
			tokenList[y].setSelectedIndex(y);
			mainPanel.add(tokenList[y]);
		}
		
		okayButton = new JButton("Okay");
		okayButton.setMnemonic(KeyEvent.VK_M);
		okayButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okayButtonActionPerformed(evt);
            }
        });
		
		cancelButton = new JButton("Cancel");
		cancelButton.setMnemonic(KeyEvent.VK_M);
		cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
		
		mainPanel.add(okayButton);
		mainPanel.add(cancelButton);
		
		frame.getContentPane().add(mainPanel, BorderLayout.PAGE_START);
		
		frame.pack(); 
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		
        setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
	}
	
	public void actionPerformed(ActionEvent e) {
        
    }
	
	private void okayButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
		
		int[] tokens = new int[board.getPlayerCount()];
		String[] names = new String[board.getPlayerCount()];
		
		for (int y = 0; y < board.getPlayerCount(); y++) {
			
			tokens[y] = tokenList[y].getSelectedIndex();
			names[y] = playerName[y].getText();
		}
		
		board.setTokens(tokens);
		board.setPlayerNames(names);
		
		if (board.errorToken() == false && board.errorName() == false) {
			
			Player[] players = new Player[board.getPlayerCount()];
			board.setPlayers(players);
			board.putData();
						
			new TurnDeterminerGUI(board);
			frame.setVisible(false);
		}
		
		else {
			
			JOptionPane.showMessageDialog(this, board.getErrorMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
    }
	
	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
		System.exit(0);
    }
}